<?php

namespace app\tzadmin\controller;

use app\common\controller\Backend;
use think\Validate;

/**
 * 后台首页
 * @internal
 */
class Index extends Backend
{

    protected $noNeedLogin = ['login', 'logout','register1','register2','register3'];
    protected $noNeedRight = ['index'];
    protected $layout = '';

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 后台首页
     */
    public function index()
    {
        //
        $menulist = $this->auth->getSidebar([
            'dashboard'  => 'hot',
            'auth'       => ['new', 'red', 'badge'],
            'auth/admin' => 12,
            'auth/rule'  => 4,
            'general'    => ['18', 'purple'],
        ]);
        $this->view->assign('menulist', $menulist);
        $this->view->assign('title', __('Home'));
        return $this->view->fetch();
    }

    /**
     * 管理员登录
     */
    public function login()
    {
        $url = $this->request->get('url', 'index/index');
        /*if ($this->auth->isLogin()){
            $this->error(__("You've logged in, do not login again"), $url);
            return;
        }*/

        if ($this->request->isPost()){
            $username = $this->request->post('username');
            $password = $this->request->post('password');
            $keeplogin = $this->request->post('keeplogin');
            $token = $this->request->post('__token__');
            $captcha = $this->request->post('verify_code');
            $rule = [
                'username'  => 'require|length:3,30',
                'password'  => 'require|length:3,30',
                '__token__' => 'token',
                //'verify_code' => 'require|captcha',
            ];
            $data = [
                'username'  => $username,
                'password'  => $password,
                '__token__' => $token,
                //'verify_code' => $captcha,
            ];
            $validate = new Validate($rule);
            $result = $validate->check($data);
            if (!$result)
            {
                $this->error($validate->getError());
                return;
            }
            $result = $this->auth->login($username, $password, $keeplogin ? 86400 : 0);
            if ($result === true)
            {
                $this->success(__('Login successful'), $url);
                return;
            }
            else
            {
                $this->error(__('Username or password is incorrect'), $url);
            }
            return;
        }

        // 根据客户端的cookie,判断是否可以自动登录
        if ($this->auth->autologin())
        {
            $this->redirect($url);
        }
        $this->view->assign('title', __('Login'));
        return $this->view->fetch();
    }

    /**
     * 注销登录
     */
    public function logout()
    {
        $this->auth->logout();
        $this->success(__('Logout successful'), 'index/login');
        return;
    }

    /**
     * 用户注册
     * 注册第一步register1
     */
    public function register1(){

        if($this->request->post()) {
            $username = $this->request->post('username');
            $password = $this->request->post('password');
            $cpassword = $this->request->post('cpassword');
            $customer = $this->request->post('customer');
            $ctrads = $this->request->post('ctrads');
            $iphone = $this->request->post('iphone');
            $token = $this->request->post('__token__');
            $captcha = $this->request->post('verify_code');

            //验证数据的规则
            $rule = [
                'username' => 'require|length:3,30',
                'password' => 'require|length:3,30',
                'cpassword' => 'require|length:3,30',
                'customer' => 'require|length:3,30',
                'ctrads' => 'require|length:3,30',
                'iphone' => 'require|number|length:3,30',
                '__token__' => 'token',
                'verify_code' => 'require|captcha',
            ];
            //数据
            $data = [
                'username' => $username,
                'password' => $password,
                'cpassword' => $cpassword,
                'customer' => $customer,
                'ctrads' => $ctrads,
                'iphone' => $iphone,
                '__token__' => $token,
                'verify_code' => $captcha,
            ];
            //$validate = new Validate($rule);
            //$result = $validate->check($data);
            /*if (!$result)
            {
                $this->error($validate->getError());
                return;
            }*/
            //print_r($this->request->module());
            //print_r($this->request->controller());

            header("Location:register2");
            exit;
        }


        return $this->view->fetch();
    }

    public function register2(){
        if($this->request->isPost()){
            header("Location:register3");exit;
        }
        return $this->view->fetch();
    }

    public function register3(){
        if($this->request->isPost()){
            header("Location:register4");exit;
        }
        return $this->view->fetch();
    }

    public function register4(){
        if($this->request->isPost()){
            $url = $this->request->get('url', 'index/index');

            $username = $this->request->post('username');
            $password = $this->request->post('password');
            $keeplogin = 1;//$this->request->post('keeplogin');
            $token = $this->request->post('__token__');

            $result = $this->auth->login($username, $password, $keeplogin ? 86400 : 0);
            if ($result === true) {
                $this->success(__('Login successful'), $url);
                return;
            }else{
                $this->error(__('Username or password is incorrect'), $url);
                return;
            }
            //header("Location:login");exit;
        }
        return $this->view->fetch();
    }

    /**
     * 获取公告列表
     * @param
     */
    public function  news(){
        header('content-type:application/json;charset=utf8');
        $news =  array(
            "url" => "http://127.0.0.4",
            'new' => 1,
            'newslist' => array(
                array(
                    'id'    => "1",
                    "title" => "天择智慧城市上线",
                    "url"   => "#",
                    "createtime"  => "1493605158",
                    "icon"     => "fa fa-warning text-yellow",
                ),
                array(
                    'id'    => "2",
                    "title" => "天择智慧城市上线2",
                    "url"   => "#",
                    "createtime"  => "1493605158",
                    "icon"     => "fa fa-warning text-yellow",
                ),
            )
        );
        echo $json_arr = json_encode($news);
        //$jsoncallback = htmlspecialchars($_REQUEST ['callback']);
        //$newstr = '{"url":"http:\/\/doc.fastadmin.net","new":1,"newslist":[{"id":"7","title":"天择智慧城市上线","url":"#","createtime":"1493605158","icon":"fa fa-warning text-yellow"}]}';
        //die($newstr);
        //echo  $json_arr;
        //echo  'callback(' . $json_arr .')';  //返回格式，必需
        exit;
    }

}
